package nc.ui.gl.assiobill.handler;

import java.util.List;

import nc.bs.framework.common.NCLocator;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.ui.pubapp.uif2app.event.IAppEventHandler;
import nc.ui.pubapp.uif2app.event.card.CardBodyAfterEditEvent;
import nc.vo.gl.assiobill.AssIOBillVO;
import nc.vo.pub.BusinessException;

public class BodyAfterEditHandler implements IAppEventHandler<CardBodyAfterEditEvent>{

  @Override
  public void handleAppEvent(CardBodyAfterEditEvent e) {
    
	  if(AssIOBillVO.PK_ASSOA_IN.equals(e.getKey())) {
		  accassNameShow(e,  AssIOBillVO.ACCASSNAME);
	  }else if(AssIOBillVO.PK_ASSOA_OUT.equals(e.getKey())) {
		  accassNameShow(e,  AssIOBillVO.OUTACCASSNAME);
	  }
	  
  }
  
  private void accassNameShow(CardBodyAfterEditEvent e, String accassFieldName) {
	  String accassname = null;
	  	
	    if(e.getValue() != null) {
	    	StringBuffer strbuf = new StringBuffer();

			String sql = "select item.name from bd_accassitem item where item.pk_accassitem in (SELECT bd_accass.pk_entity FROM bd_accass WHERE pk_accasoa = '"+e.getValue()+"')";

			List<String> result = null;

			try {
				result = (List<String>) NCLocator.getInstance().lookup(IUAPQueryBS.class).executeQuery(sql, new ColumnListProcessor());
			} catch (BusinessException e1) {
				e1.printStackTrace();
			}
			
			if(result != null && result.size() > 0) {
				
				int len = result.size();
				
				for(int i = 0; i < len; i ++) {
					strbuf.append(result.get(i));
					if(i != len - 1) {
						strbuf.append(",");
					}
				}
				accassname = strbuf.toString();
			}
	    }
	    
	    e.getBillCardPanel().getBillModel().setValueAt(accassname, e.getRow(), accassFieldName);
  }

}
